//
//  AppDelegate.m
//  XPlayer
//
//  Created by liguoqing on 2023/8/2.
//

#import "AppDelegate.h"
#import "CYLTabBarControllerConfig.h"
#import <HYUtls/HYAppThemeManager.h>
#import "HYMineSetManager.h"
#import <Bugly/Bugly.h>
#import "HYProtocolAlerView.h"
#import <HYUIKit/HYWKWebViewController.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self setupRootController];
    [self reachabilityChanged];
    [self headerviewAdaptedToTableView];
    [self initSetParameters];
    [self initBuglySDK];
    [self showAlerAgreement];
    return YES;
}

- (void)setupRootController
{
    [self setNavigationBarThemeColor];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#FFFFFF" darkModeHex:@"#0E0E0E"];
    CYLTabBarControllerConfig *tabBarControllerConfig = [[CYLTabBarControllerConfig alloc] init];
    CYLTabBarController *tabBarController = tabBarControllerConfig.tabBarController;
    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];
}

- (void)setNavigationBarThemeColor
{
    // 主题色 #F8B62D
    [[HYAppThemeManager shareManager] setAPPNavigaionBgLightColor:@"#F8B62D" // 导航栏背景色
                                             navigaionBgDarkColor:@"#24262E" // @"#191919"
                                                   statusBarStyle:UIStatusBarStyleDefault
                                               tabBarBgLightColor:@"#ECECEC" //tabBar底色
                                                tabBarBgDarkColor:@"#24262E" // @"#1C1C1C"
                                                appMainLightColor:@"#F8B62D" // 主题色
                                                 appMainDarkColor:@"#F8B62D"
                                              contentBgLightColor:@"#FFFFFF" // APP内容底层
                                               contentBgDrakColor:@"#24262E" //@"#202020"
                                                  appBgLightColor:@"#F5F5F5" // 主页面背景色
                                                   appBgDrakColor:@"#181A22" //#111111
                                                   textLightColor:@"#222222" // 字体颜色
                                                    textDrakColor:@"#DAE0F2" //@"#FFFFFF"
                                                 selectLightColor:@"#F8B62D" // 选择颜色
                                                  selectDrakColor:@"#F8B62D"];
}

- (void)reachabilityChanged
{
    [[HYNetWorkManager shareManager] startNotifier];
}

// 初始化设置参数
- (void)initSetParameters
{
    [HYMineSetManager firstInstallSetInitialParameters];
}

- (void)initBuglySDK
{
    [BuglyLog initLogger:BuglyLogLevelDebug consolePrint:true];
    BLYLogDebug(@"Debug");
    BLYLogError(@"Warn");
    BuglyConfig *bgConfig = [[BuglyConfig alloc] init];
    //todo 上线后修改为NO
    bgConfig.debugMode = NO;
    bgConfig.blockMonitorEnable = NO;
    bgConfig.blockMonitorTimeout = 5;
    bgConfig.reportLogLevel = BuglyLogLevelError;
    [Bugly startWithAppId:@"e642fe73b3" config:bgConfig];
}

- (void)showAlerAgreement
{
    if([HYMineSetManager shareManager].isAccepAgreement){
        return;
    }
    @weakify(self);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        HYProtocolAlerView *alertView = [[HYProtocolAlerView alloc]init];
        alertView.content =
        NSLocalizedString(@"欢迎使用VPlayer！\n为了更好地保障您的个人权益，请认真阅读《用户协议》和《隐私政策》的全部内容，同意并接受全部条款后开始使用我们的产品和服务。\n若不选择同意，将无法使用我们的产品和服务，并退出应用。", nil);
        [alertView showAlert:[UIApplication sharedApplication].delegate.window.rootViewController cancelAction:^{
            [HYMineSetManager exitApplication];
        } privateAction:^{
            @strongify(self);
            [self showPrivacyAgreement];
        } userAction:^{
            @strongify(self);
            [self showUserAgreement];
        }];
    });
}

#pragma mark - 用户协议
- (void)showUserAgreement
{
    HYWKWebViewController *vc = [[HYWKWebViewController alloc]init];
    vc.hiddenNavigationBar = YES;
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"UserAgreement" ofType:@"html" inDirectory:nil];
    vc.localUrl = filePath;
    [[CommUtls getCurrentVC] presentViewController:vc animated:YES completion:nil];
}
#pragma mark - 隐私协议
- (void)showPrivacyAgreement
{
    HYWKWebViewController *vc = [[HYWKWebViewController alloc]init];
    vc.hiddenNavigationBar = YES;
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"PrivacyAgreement" ofType:@"html" inDirectory:nil];
    vc.localUrl = filePath;
    [[CommUtls getCurrentVC] presentViewController:vc animated:YES completion:nil];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
    return YES;
//    return [[BDPanOauthManager manager] handleOpenURL:url];
}



- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

// iOS 15.0 适配TabView 默认表头 区头 高度问题
- (void)headerviewAdaptedToTableView 
{
    if (@available(iOS 15.0, *)) {
        [UITableView appearance].sectionHeaderTopPadding = 0;
    }
}

@end
